package com.itheima.mp.controller;


import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/users/")
@Api(tags = "这是用户管理接口")
@RequiredArgsConstructor
public class UserController {
    //@Autowired
    private final UserService userService;


    @ApiOperation("新增用户接口")
    @PostMapping()
    public void saveUser(@RequestBody UserFormDTO userFormDTO){
        //此处由于swagger mocker 测试不返回result数据
        User user=new User();
        BeanUtils.copyProperties(userFormDTO,user);

        userService.save(user);
    }

    @ApiOperation("删除接口")
    @DeleteMapping("{id}")
    public void deleteUser(@ApiParam("用户id") @PathVariable("id") Long id){
        User user=new User();
        userService.removeById(id);
    }

    @ApiOperation("查询接口")
    @GetMapping("{id}")
    public UserVO QueryUser(@ApiParam("用户id") @PathVariable("id") Long id){
        User user =  userService.getById(id);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO);
        return userVO;
    }

    @ApiOperation("批量查询接口")
    @GetMapping
    public List<UserVO> QueryUserList(@ApiParam("用户ids") @RequestParam("ids") List<Long> ids){
       
        List<User> user =  userService.listByIds(ids);
//        List<UserVO> userVO = List.of();
        return BeanUtil.copyToList(user,UserVO.class);

    }

    @ApiOperation("扣减用户余额接口")
    @PutMapping("{id}/deduction/{money}")
    public void updateBalance(@PathParam ("id") @PathVariable Long id, @PathParam ("amount") @PathVariable Long money){
        userService.updateMoney(id,money);
    }

    @ApiOperation("复杂条件扣减用户余额接口")
    @PutMapping("list")
    public List<UserVO> updateBalanceComplex(@RequestBody UserQuery userQuery){
        List<User> users = userService.queryUsers(userQuery.getName(),userQuery.getStatus(),userQuery.getMinBalance(),userQuery.getMaxBalance());
        return BeanUtil.copyToList(users,UserVO.class);
    }

}
